﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Standard.Core
{
    /// <summary>
    /// Base 64 helper class
    /// </summary>
    public class Base64
    {
        /// <summary>
        /// Convert string to Base64 string
        /// </summary>
        /// <param name="text">Input text</param>
        /// <returns>Base64 string</returns>
        public string EncodeBase64(string text)
        {
            if (text == null)
            {
                throw new ArgumentNullException("Text");
            }
            else
            {
                return Convert.ToBase64String(Encoding.UTF8.GetBytes(text));
            }
        }

        /// <summary>
        /// Convert string from Base64
        /// </summary>
        /// <param name="base64String">Base64 string</param>
        /// <returns>decoded Base64 string</returns>
        public string DecodeBas64(string base64String)
        {
            if (base64String == null)
            {
                throw new ArgumentNullException("Base64String");
            }
            else
            {
                return Encoding.UTF8.GetString(Convert.FromBase64String(base64String));
            }
        }

        /// <summary>
        /// Check wether the input is in Base64 format
        /// </summary>
        /// <param name="base64String">Input string</param>
        /// <returns>True if the string is in Base64 format</returns>
        public bool IsStringBase64(string base64String)
        {
            try
            {
                Convert.FromBase64String(base64String);
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}
